package com.eunion.web.cms.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eunion.common.util.CommUtils;
import com.eunion.common.util.RegUtils;
import com.eunion.core.LogUtil;
import com.eunion.core.common.DeleteVo;
import com.eunion.core.common.JsonVo;
import com.eunion.core.exception.ValidateException;
import com.eunion.web.cms.entity.BaoKaoGuide;
import com.eunion.web.cms.service.BaoKaoGuideService;
import com.eunion.web.common.base.BaseService;
import com.eunion.web.common.base.BaseTableCommonController;
import com.eunion.web.common.vo.Web;

/**
 * 报考指南管理
 */
@Controller
@RequestMapping("/api/baokao-guide")
public class BaoKaoGuideResource extends BaseTableCommonController<BaoKaoGuide, Long> {

    @Autowired
    private BaoKaoGuideService baoKaoGuideService;

    @Override
    protected BaseService<BaoKaoGuide, Long> getBaseService() {
        return baoKaoGuideService;
    }

    /**
     * 进入报考指南列表页面
     * @return
     */
    @RequestMapping("/index")
    public String index() {
        return getUrlBase();
    }

    @Override
    protected String getUrlBase() {
        return "admin/baokaoguide/baokao-guide";
    }

    /**
     * 保存对象
     * @param obj
     * @param result
     * @return
     */
    @PreAuthorize("hasAuthority('baokao:guide:add')")
    @PostMapping(value = Web.URL_SEP_SAVE)
    public String save(BaoKaoGuide obj, BindingResult result, Model model) {
        try {
            //对数据进行校验
            if (obj != null) {
                if (!RegUtils.StrLess(obj.getTitle(), 256)) {
                    setModuleStatusError(model, "标题长度不能超过255");
                    setModuleAdd(model);
                    return getUrlBase().concat("-add");
                }
            }
            if (result.hasErrors()) {
                List<ObjectError> list = result.getAllErrors();
                for (ObjectError error : list) {
                    if (StringUtils.isNotEmpty(error.getDefaultMessage())) {
                        throw new ValidateException(error.getDefaultMessage());
                    }
                }
            }

            //后台添加创建时间，和发布时间
            obj.setCreateTime(System.currentTimeMillis());
            obj.setImgUrl(CommUtils.getFirstImgUrl(obj.getContent()));
            setModuleStatusOK(model);
            getBaseService().save(obj);
            setModuleStatusOK(model, "新增成功");
        } catch (ValidateException ve) {
            setModuleStatusError(model, ve.getMsg());
        } catch (Exception e) {
            LogUtil.get(this.getClass()).error("save obj error : {}", e);
            setModuleStatusError(model, "新增失败");
        }
        setModuleAdd(model);
        return getUrlBase().concat("-add");
    }

    /**
     * 根据传入的数据ids,来删除对象.
     */
    @PostMapping(value = Web.URL_SEP_REMOVE)
    @ResponseBody
    protected JsonVo remove(@RequestBody DeleteVo<Long> dvo) {
        try {
            if (dvo != null && dvo.getIds() != null && dvo.getIds().size() > 0) {
                return super.remove(dvo);
            }
            return new JsonVo(true);
        } catch (Exception e) {
            LogUtil.get(this.getClass()).error("remove error ", e);
            return new JsonVo(false, "删除失败");
        }
    }

    /**
     * 查看详情
     * @param id
     * @param model
     * @return
     */
    @GetMapping(value = "/detail/{id}")
    public String detail(@PathVariable Long id, Model model) {
        setModuleEdit(model);
        BaoKaoGuide cur = baoKaoGuideService.findOne(id);
        if (cur == null) {
            setModuleAdd(model);
            setModuleStatusError(model, "该条记录不存在!");
            model.addAttribute(Web.OBJ, null);
            return getUrlBase().concat("-detail");
        }
        setModuleAdd(model);
        model.addAttribute(Web.OBJ, cur);
        return getUrlBase().concat("-detail");
    }
}
